using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_OneItem : UIBaseNode
    {
        #region Template Generate,don't modify
        protected partial class UIB_UIN_OneItem
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExArtSpriteNum imageQuality { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage imageIcon { protected set; get; }
            public UnityEngine.GameObject imageGray { protected set; get; }
            public UnityEngine.GameObject lv { protected set; get; }
            public UnityEngine.GameObject star { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textLv { protected set; get; }
            public UnityEngine.GameObject countRoot { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textCount { protected set; get; }
            public UnityEngine.GameObject costOrHave { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textHave { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textNeed { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textName { protected set; get; }
            public UnityEngine.GameObject selectRoot { protected set; get; }
            public UnityEngine.GameObject equipType { protected set; get; }
            public IQIGame.Onigao.Framework.ExArtSpriteNum iconEquipType { protected set; get; }
            public UnityEngine.GameObject tagWear { protected set; get; }
            public UnityEngine.GameObject tagOtherWearer { protected set; get; }
            public UnityEngine.GameObject tagLock { protected set; get; }
            public UnityEngine.GameObject tagWearCurr { protected set; get; }
            public UnityEngine.GameObject tagWearRepeat { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage iconOtherWearer { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("imageQuality", out var __tbv0);
                this.imageQuality = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("imageIcon", out var __tbv1);
                this.imageIcon = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("imageGray", out var __tbv2);
                this.imageGray = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("lv", out var __tbv3);
                this.lv = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("star", out var __tbv4);
                this.star = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textLv", out var __tbv5);
                this.textLv = __tbv5;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("countRoot", out var __tbv6);
                this.countRoot = __tbv6;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textCount", out var __tbv7);
                this.textCount = __tbv7;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("costOrHave", out var __tbv8);
                this.costOrHave = __tbv8;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textHave", out var __tbv9);
                this.textHave = __tbv9;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textNeed", out var __tbv10);
                this.textNeed = __tbv10;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textName", out var __tbv11);
                this.textName = __tbv11;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("selectRoot", out var __tbv12);
                this.selectRoot = __tbv12;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("equipType", out var __tbv13);
                this.equipType = __tbv13;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("iconEquipType", out var __tbv14);
                this.iconEquipType = __tbv14;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("tagWear", out var __tbv15);
                this.tagWear = __tbv15;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("tagOtherWearer", out var __tbv16);
                this.tagOtherWearer = __tbv16;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("tagLock", out var __tbv17);
                this.tagLock = __tbv17;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("tagWearCurr", out var __tbv18);
                this.tagWearCurr = __tbv18;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("tagWearRepeat", out var __tbv19);
                this.tagWearRepeat = __tbv19;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("iconOtherWearer", out var __tbv20);
                this.iconOtherWearer = __tbv20;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        private ListScrollAdapter<UIN_CommonDarkLight> stars;
        protected UIB_UIN_OneItem ui { get; set; }

        private int currStar = 0;
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_OneItem();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.stars = this.InitListScroll<UIN_CommonDarkLight>(this.ui.star, this.OnStarChanged);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {

        }

        private void SetUnitDefualt()
        {
            this.SetActiveCount(false);
            this.SetActiveCost(false);
            this.SetActiveLv(false);

            this.SetGray(false);
            this.SetSelected(false);

            this.SetActiveEquipClassType(false);
            this.SetActiveEquipTagLock(false);
            this.SetActiveEquipTagOtherWearer(false);
            this.SetActiveEquipTagWear(false);
            this.SetActiveEquipTagWearCurr(false);
            this.SetActiveEquipTagWearRepeart(false);
        }

        public bool Show(CfgItem cfg)
        {
            if (cfg == null)
            {
                this.Hide();
                return false;
            }
            this.Show();
            this.SetUnitDefualt();
            this.ShowStar(0);
            this.SetName(cfg.Name);
            this.SetIcon(cfg.Icon_fullPath);
            this.SetQuality(cfg.Quality);
            return true;
        }

        public bool Show(int itemCid, int itemNum)
        {
            CfgItem cfgItem = TableCenter.item.Get(itemCid);
            bool isOkey = this.Show(cfgItem);
            if (isOkey)
            {
                this.SetCount(itemNum, itemNum > 1);
            }
            else
            {
                this.Hide();
            }
            return isOkey;
        }

        private void SetActiveCount(bool isActive)
        {
            this.ui.countRoot.SetActive(isActive);
        }

        private void SetActiveCost(bool isActive)
        {
            this.ui.costOrHave.SetActive(isActive);
        }

        private void SetActiveLv(bool isActive)
        {
            this.ui.lv.SetActive(isActive);
        }

        private void SetCost(int numHas, int numCost)
        {
            int indexNumHas = 2;
            string strNeed = "";
            string strHave = numHas.ToString();
            if (numCost > 0)
            {
                strNeed = "/" + numCost;
                indexNumHas = numCost <= numHas ? 1 : 0;
            }

            this.ui.textNeed.text = strNeed;
            this.ui.textHave.text = strHave;
            //this.ui.textHave.indexSwitchColor = indexNumHas;
        }

        public void SetCost(int numHas, int numCost, bool isActive)
        {
            this.SetCost(numHas, numCost);
            this.SetActiveCost(isActive);
        }

        public void SetName(string name)
        {
            this.ui.textName.text = name;
        }

        private void SetCount(int num)
        {
            this.ui.textCount.text = "x" + num;
        }

        public void SetCount(int num, bool isActive)
        {
            this.SetCount(num);
            this.SetActiveCount(isActive);
        }

        public void ShowLv(int lv)
        {
            this.SetLv(lv);
            this.SetActiveLv(true);
        }

        private void SetLv(int lv)
        {
            this.ui.textLv.text = string.Format("{0:00}", lv);
        }

        public void SetIcon(string img)
        {
            this.SetImageSprite(this.ui.imageIcon, img);
        }

        public void SetQuality(int quality)
        {
            this.ui.imageQuality.SetInt(quality);
        }

        public void SetGray(bool isGray)
        {
            this.ui.imageGray.SetActive(isGray);
        }

        public void SetSelected(bool isSelected)
        {
            this.ui.selectRoot.SetActive(isSelected);
        }

        #region 装备Item
        public void ShowEquip(ItemEquipmentData equipItem, HeroData heroData, int currColumn)
        {
            if (!this.ShowEquip(equipItem))
                return;

            int wearColumn = heroData.GetWearEquipColumn(equipItem);
            if (wearColumn > 0 && wearColumn == currColumn)
            {
                this.SetActiveEquipTagWearCurr(true);
                return;
            }

            HeroData wearer = equipItem.currWearer;
            if (wearer != null)
            {
                bool isWearByOther = wearer != heroData;
                if (isWearByOther)
                {
                    this.SetEquipOtherWearerIcon(wearer.cfgSkin.HeroHeadImage_fullPath);
                }
                this.SetActiveEquipTagOtherWearer(isWearByOther);
                this.SetActiveEquipTagWear(!isWearByOther);
                return;
            }

            int wearSameCidEquipColumn = heroData.GetWearSameCidEquipColumn(equipItem);
            if (wearSameCidEquipColumn != -1)
            {
                this.SetActiveEquipTagWearRepeart(true);
                return;
            }
        }

        public bool ShowEquip(ItemEquipmentData equipItem)
        {
            var cfg = equipItem.cfgData;
            if (!this.Show(cfg))
                return false;
            this.SetActiveEquipTagLock(equipItem.isLock);
            this.SetEquipClassType(cfg.SubType);
            this.ShowStar(equipItem.star, equipItem.maxStar);
            this.ShowLv(equipItem.level);
            return true;
        }

        private void ShowStar(int currStar, int maxStar = 0)
        {
            this.currStar = currStar;
            this.stars.RefillCells(maxStar);
        }

        private void OnStarChanged(UIN_CommonDarkLight item, int index)
        {
            bool isLight = this.currStar <= (index + 1);
            item.Show(isLight);
        }

        private void SetActiveEquipClassType(bool isActive)
        {
            this.ui.equipType.SetActive(isActive);
        }

        private void SetEquipClassType(int equipClassType, bool isActive = true)
        {
            if (isActive)
            {
                this.ui.iconEquipType.SetInt(equipClassType);
            }
            this.SetActiveEquipClassType(isActive);
        }

        private void SetActiveEquipTagWear(bool isActive)
        {
            this.ui.tagWear.SetActive(isActive);
        }

        private void SetActiveEquipTagWearCurr(bool isActive)
        {
            this.ui.tagWearCurr.SetActive(isActive);
        }

        private void SetActiveEquipTagWearRepeart(bool isActive)
        {
            this.ui.tagWearRepeat.SetActive(isActive);
        }

        private void SetActiveEquipTagOtherWearer(bool isActive)
        {
            this.ui.tagOtherWearer.SetActive(isActive);
        }

        private void SetEquipOtherWearerIcon(string heroIconAssetPath)
        {
            this.SetImageSprite(this.ui.iconOtherWearer, heroIconAssetPath);
        }

        private void SetActiveEquipTagLock(bool isActive)
        {
            this.ui.tagLock.SetActive(isActive);
        }
        #endregion
    }
}

